# -*- coding: utf-8 -*-

from __future__ import absolute_import
import datetime 
import os, subprocess, redis

from django.conf import settings
from celery.utils.log import get_task_logger
from financial_daily.celery import app

from basic_info.models import Stock
from companies.models import QA 

from financial_daily.moneyflow_processors import MoneyflowProcessor
from financial_daily.utils import RedisUtils 
from financial_daily.news_classifiers import NewsClassifiers
from financial_daily.qa_statistics import QAStatistics
from financial_daily.polarity_check import PolarityCheck
from financial_daily.xml_processors import ChuanwenProcessor

logger = get_task_logger(__name__)
date_str = datetime.date.today().strftime('%Y-%m-%d')

@app.task(queue='web_access')
def process_daily_chuanwen(date=date_str):
    processor = ChuanwenProcessor()
    processor.process(date_str)

@app.task
def sync_moneyflow(date=date_str):
    # 同步资金流信息
    base_dir = settings.MONEYFLOW_SERVICE['daily_file_dir']
    rsync_connection_str = settings.MONEYFLOW_SERVICE['rsync_connection_str']
    data_path = os.path.join(base_dir, date)

    if not os.path.exists(data_path):
        os.mkdir(data_path)

    cmd = ['rsync', '-avzP', rsync_connection_str, data_path]

    subprocess.call(cmd)
    generate_daily_moneyflow_stats(date)

def generate_daily_moneyflow_stats(date):
    # 生成每日资金流统计数据
    moneyflow_processor = MoneyflowProcessor()
    count = moneyflow_processor.process(date)
    if count == 0:
      logger.error("No moneyflow data added!")
    else:
      logger.info("Done processing daily moneyflow data. %d added.", count)

@app.task    
def import_daily_qa(date=date_str):
    news_classifiers = NewsClassifiers()
    news_polarity = PolarityCheck()

    redis_host = settings.QA_REDIS_HOST
    redis_port = settings.QA_REDIS_PORT
    redis_db_no = settings.QA_REDIS_DB_NO
    redis_conn = RedisUtils.connect_redis(host=redis_host, port=redis_port, db=redis_db_no)
    cur_date = "QA." + date
    members = redis_conn.smembers(cur_date)
    for member in members:
        question_date = redis_conn.hget(member, 'questionedAt')
        answer_date = redis_conn.hget(member, 'replyTime')
        stockcode = redis_conn.hget(member, 'stockcode')
        question = redis_conn.hget(member, 'question')
        answer = redis_conn.hget(member, 'reply') 
        main_cat, sub_cat = news_classifiers.classify(question.decode(settings.DEFAULT_ENCODING))
        polarity = news_polarity.get_text_polarity(question.decode(settings.DEFAULT_ENCODING))[0]
        try:
            stock = Stock.objects.get(code=stockcode)
            QA.objects.get_or_create(question_date=question_date,
                                     stock=stock,
                                     question=question,
                                     defaults={
                                         'answer': answer,
                                         'answer_date': answer_date, 
                                         'main_category': main_cat, 
                                         'sub_category': sub_cat,
                                         'polarity': polarity,
                                     })
        except Stock.DoesNotExist:
            logger.warn("Stock code %s does not exist!", stockcode)
    # 统计当日信息
    get_qa_statistics(date)

@app.task
def get_qa_statistics(date=date_str, cycle=1):
    cur_date = datetime.datetime.strptime(date,"%Y-%m-%d")
    qa_statistics = QAStatistics(cur_date, cycle)

    qa_statistics.get_qa_statistics()

